﻿package com.gengine.manager
{
    import flash.external.*;
    import flash.net.*;

    public class BrowerManager extends Object
    {
        private static var _instance:BrowerManager;

        public function BrowerManager()
        {
            if (_instance != null)
            {
                throw "BrowerManager 为单例";
            }
            return;
        }// end function

        public function get url() : String
        {
            if (!ExternalInterface.available)
            {
                return null;
            }
            try
            {
                return ExternalInterface.call("Brower.getURL");
            }
            catch (e:Error)
            {
            }
            return "";
        }// end function

        public function get baseUrl() : String
        {
            if (!ExternalInterface.available)
            {
                return null;
            }
            var _loc_1:* = this.url;
            var _loc_2:* = _loc_1.indexOf("#");
            if (_loc_2 > 0)
            {
                return _loc_1.substr(0, (_loc_2 - 1));
            }
            return _loc_1;
        }// end function

        public function asCallJsFun(param1:String) : Boolean
        {
            var jsFun:* = param1;
            if (!ExternalInterface.available)
            {
                return false;
            }
            try
            {
                ExternalInterface.call(jsFun);
            }
            catch (e:Error)
            {
                return false;
            }
            return true;
        }// end function

        public function asCallJsFunParam(param1:String, ... args) : Boolean
        {
            args = new activation;
            var jsFun:* = param1;
            var rest:* = args;
            if (!ExternalInterface.available)
            {
                return false;
            }
            try
            {
                switch(length)
                {
                    case 0:
                    {
                        ExternalInterface.call();
                        break;
                    }
                    case 1:
                    {
                        ExternalInterface.call(, [0]);
                        break;
                    }
                    case 2:
                    {
                        ExternalInterface.call(, [0], [1]);
                        break;
                    }
                    case 3:
                    {
                        ExternalInterface.call(, [0], [1], [2]);
                        break;
                    }
                    case 4:
                    {
                        ExternalInterface.call(, [0], [1], [2], [3]);
                        break;
                    }
                    case 5:
                    {
                        ExternalInterface.call(, [0], [1], [2], [3], [4]);
                        break;
                    }
                    default:
                    {
                        break;
                    }
                }
            }
            catch (e:Error)
            {
                return false;
            }
            return true;
        }// end function

        public function set title(param1:String) : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            try
            {
                ExternalInterface.call("Brower.setTitle", param1);
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public function get title() : String
        {
            if (!ExternalInterface.available)
            {
                return null;
            }
            try
            {
                return ExternalInterface.call("Brower.getTitle");
            }
            catch (e:Error)
            {
            }
            return "";
        }// end function

        public function set urlVariables(param1:URLVariables) : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            var _loc_2:String = "";
            var _loc_3:* = param1.toString();
            if (_loc_3.length > 0)
            {
                _loc_2 = _loc_2 + ("#" + _loc_3);
            }
            try
            {
                ExternalInterface.call("Brower.setUrlVariables", _loc_2);
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public function get urlVariables() : URLVariables
        {
            if (!ExternalInterface.available)
            {
                return null;
            }
            var _loc_1:* = this.url;
            var _loc_2:* = _loc_1.indexOf("#");
            if (_loc_2 > 0)
            {
                return new URLVariables(_loc_1.substr((_loc_2 + 1)));
            }
            return new URLVariables();
        }// end function

        public function addFavorite(param1:String = null, param2:String = null) : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            if (!param2)
            {
                param2 = this.url;
            }
            if (!param1)
            {
                param1 = this.title;
            }
            try
            {
                ExternalInterface.call("Brower.addFavorite", param2, param1);
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public function setHomePage(param1:String = null) : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            if (!param1)
            {
                param1 = this.url;
            }
            try
            {
                ExternalInterface.call("Brower.setHomePage", param1);
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public function setCookie(param1:String, param2:String, param3:Date = null, param4:Boolean = false) : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            var _loc_5:* = new Date(new Date().time + 1000 * 86400 * 365);
            param3 = new Date(new Date().time + 1000 * 86400 * 365);
            try
            {
                ExternalInterface.call("Brower.setCookie", param1, param2, param3.time, param4);
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public function getCookie(param1:String) : String
        {
            if (!ExternalInterface.available)
            {
                return null;
            }
            try
            {
                return ExternalInterface.call("Brower.getCookie", param1);
            }
            catch (e:Error)
            {
            }
            return "";
        }// end function

        public function confirmClose(param1:String = "确认退出？") : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            if (param1)
            {
                try
                {
                    ExternalInterface.call("Brower.confirmClose", param1);
                }
                catch (e:Error)
                {
                }
            }
            else
            {
                try
                {
                    ExternalInterface.call("Brower.confirmClose");
                }
                catch (e:Error)
                {
                }
            }
            return;
        }// end function

        public function alert(... args) : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            try
            {
                ExternalInterface.call("alert", args.toString());
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public function reload() : void
        {
            if (ExternalInterface.available)
            {
                try
                {
                    BrowerManager.instance.confirmClose("");
                    ExternalInterface.call("location.reload");
                }
                catch (e)
                {
                }
            }
            return;
        }// end function

        public function disableScroll(param1:String = null) : void
        {
            if (!ExternalInterface.available)
            {
                return;
            }
            try
            {
                ExternalInterface.call("Brower.disableScroll", param1);
            }
            catch (e:Error)
            {
            }
            return;
        }// end function

        public function getUrl(param1:String, param2:String = "_self") : void
        {
            var _loc_3:String = null;
            if (param1.substr(0, 11) == "javascript:" && ExternalInterface.available)
            {
                _loc_3 = param1.substr(11);
                if (param1.indexOf("(") == -1 && param1.indexOf(")") == -1)
                {
                    _loc_3 = _loc_3 + "()";
                }
                try
                {
                    ExternalInterface.call("eval", _loc_3);
                }
                catch (e:Error)
                {
                }
            }
            else
            {
                navigateToURL(new URLRequest(param1), param2);
            }
            return;
        }// end function

        public function getBrowserType() : String
        {
            if (!ExternalInterface.available)
            {
                return "Other";
            }
            try
            {
                return ExternalInterface.call("Brower.getBrowserType");
            }
            catch (e:Error)
            {
            }
            return "Other";
        }// end function

        public static function get instance() : BrowerManager
        {
            if (_instance == null)
            {
                _instance = new BrowerManager;
            }
            return _instance;
        }// end function

    }
}
